
const tbody = document.querySelector('tbody');

getScoreList()
function getScoreList() {
    
axios.get('/score/list')
.then(result => {
    const obj = result.data.data
    console.log(obj);
    let html = ''
    for (const key in obj) {
        const temphtml = obj[key].score.reduce((h, v,i) => h + `<td  class="score">${v} <input data-text='${v}' data-id='${key}' data-bach='${i+1}' type="text" value='${v}'></td>`, '')
        html += `<tr>
    <th scope="row">${key}</th>
    <td>${obj[key].name}</td>
    ${temphtml}
  </tr>`
    }
    tbody.innerHTML = html

    const inputs = document.querySelectorAll('tbody input');
    inputs.forEach(input => {
        input.addEventListener('blur', function (e) {
            const data = {
                "stu_id": e.target.dataset.id,

                "batch": e.target.dataset.bach,

                "score": input.value
            }
            axios.post('/score/entry',data)
            .then(result=>{
                if(result.data.code===0){
                    toastr.success('修改成功')
                    getScoreList()
                }
                    
                
            }).catch(err=>{
                console.dir(err);
                if(err.response.status===400){
                    e.target.style.display='none'
                }
            })
        })
    })
})

}
tbody.addEventListener('dblclick', function (e) {
    if (e.target.nodeName = 'TD' && e.target.classList.contains('score')) {
        e.target.querySelector('input').style.display = 'block'
        e.target.querySelector('input').focus()

    }
})